#include "device_manage.h"
#include <etimer.h>
#include <process.h>
#include <string.h>
#include "74hc595.h"
#include "disp_task.h"
#include "flash.h"
#include "fun_flash.h"
#include "gprs_task.h"
#include "gsm.h"
#include "iwdg.h"
#include "key_task.h"
#include "lora_task.h"
#include "mmem.h"
#include "net_api.h"
#include "stm32f1xx_hal_iwdg.h"
#include "tm1804.h"
#include "uart.h"
#include "voice_task.h"
#include "wifi.h"
#include "wifi_task.h"

PROCESS(DevCheck_Process, "DeviceCheck");

uint8_t netdevice_type = net_device_none;  //网络设备类型

sSetMenu SetMenu;

const uint8_t menu_init[] = {f_start, f_voice, f_bell, f_remind,
                             f_time};  //初始化可操作功能
const uint8_t menu_single[] = {f_start, f_voice, f_bell, f_remind,
                               f_time,  f_add,   f_del};  //单机版可操作功能
const uint8_t menu_wifi[] = {f_start,  f_voice, f_bell,
                             f_remind, f_net};  // wifi版可操作功能
const uint8_t menu_gprs[] = {f_start, f_voice, f_bell, f_remind};  // gprs版可操作功能

tCfgData cfg_data;
sUID uid;
volatile eNetStatus netstatus = net_idle;
unsigned char mqttdata_fifo[32 * sizeof(sMqttData)];
FIFO_Type mqtt_fifo;

extern uint8_t led;  //氛围灯

uFeedDog FeedDog;  //喂狗标志

unsigned char FeedDog_Init(void) {
  FeedDog.bit.keydog = 1;
  FeedDog.bit.loradog = 1;

  return FeedDog.flag;
}

void Feed_KeyDog(void) { FeedDog.bit.keydog = 1; }

void Feed_LoraDog(void) { FeedDog.bit.loradog = 1; }

unsigned char Get_FeedDog(void) { return FeedDog.flag; }

void Clear_FeedDog(void) { FeedDog.flag = 0; }

tModel Model = Model_Call;  // LORA主机模式
//
void read_cfg(tCfgData *cfgdata) {
  //	ReadFlash(FLASH_CFG_ADDRESS,0,sizeof(cfgdata->Var)/sizeof(uint8_t),&cfgdata->Var[0]);
  ReadFlash(FLASH_CFG_ADDRESS, 0, 32, &cfgdata->Var[0]);
}

void write_cfg(tCfgData *cfgdata) {
  //	WriteFlash(FLASH_CFG_ADDRESS,0,sizeof(cfgdata->Var)/sizeof(uint8_t),&cfgdata->Var[0]);
  WriteFlash(FLASH_CFG_ADDRESS, 0, 32, &cfgdata->Var[0]);
}

void init_cfg(void) {
  read_cfg(&cfg_data);
  if (cfg_data.Val.SysInitFlag != Sys_Init) {  //未配置时配置默认值
    cfg_data.Val.SysInitFlag = Sys_Init;
    cfg_data.Val.WifiFlag = Wifi_NotInit;
    cfg_data.Val.Flash_index = 0;
    cfg_data.Val.RemindCnt = remind_default;
    cfg_data.Val.BellIndex = bell_default;
    cfg_data.Val.Volume = volume_default;
    cfg_data.Val.DeviceFlag = Device_NotInit;
    write_cfg(&cfg_data);  //写配置到flash
  }
  read_cfg(&cfg_data);
  if (cfg_data.Val.MacFlag == Mac_Init) {
    uid.temp0 = cfg_data.Val.Device_mac.Val.temp0;
    uid.temp1 = cfg_data.Val.Device_mac.Val.temp1;
    uid.temp2 = cfg_data.Val.Device_mac.Val.temp2;
  } else {
    uid.temp0 = *(__IO unsigned int *)(0x1FFFF7E8);
    uid.temp1 = *(__IO unsigned int *)(0x1FFFF7EC);
    uid.temp2 = *(__IO unsigned int *)(0x1FFFF7F0);
  }
}

//#define test_debug
PROCESS_THREAD(DevCheck_Process, ev, data) {
  static sLEDData set_data;
  static struct etimer et;
  static uint8_t pin0;
  static uint8_t pin1;

  PROCESS_BEGIN();

  lora_init();
  Model = Model_Call;
  menb_pack_init();
  led = Led_DevInit;
  process_start(&Disp_Process, NULL);                     //显示任务
  process_start(&DispLed_Process, (process_data_t)&led);  //氛围灯任务

  init_cfg();         //读取flash数据
  get_sflash_addr();  //从机flash 操作地址

  process_start(&LORA_Process, NULL);  // LORA任务
  process_start(&Key_Process, NULL);   //开始按键检测任务

  SetMenu.count = sizeof(menu_init) / sizeof(menu_init[0]);
  SetMenu.menu = (uint8_t *)menu_init;  //设置菜单为初始化模式

//#ifdef test_debug
//  cfg_data.Val.MacFlag = Mac_Init;
//#endif
  if (cfg_data.Val.MacFlag != Mac_Init) {  //未添加二维码
    set_data.cmd = disp_factory;
    process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
	printf("have not mac\r\n");
  } else {
	printf("have mac\r\n");
    process_start(&NVCPlay_Process, NULL);

    pin0 = HAL_GPIO_ReadPin(Check0_GPIO_Port, Check0_Pin);
    pin1 = HAL_GPIO_ReadPin(Check1_GPIO_Port, Check1_Pin);
    if (pin0 == GPIO_PIN_RESET && pin1 == GPIO_PIN_SET) {  // gsm
	  printf("gsm version\r\n");
      netdevice_type = net_device_gsm;

	  gsm_rsthign();
      gsm_off();
      etimer_set(&et, 2500);
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
      gsm_rstlow();
      gsm_on();
      etimer_set(&et, 5000);
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

      NetApi_Init(Net_Gsm);
      process_start(&GprsInit_Process, NULL);
      FIFO_Init(&mqtt_fifo, mqttdata_fifo, sizeof(sMqttData), 12);
	  Gsm_ATE(1);
      SetMenu.count = sizeof(menu_gprs) / sizeof(menu_gprs[0]);
      SetMenu.menu = (uint8_t *)menu_gprs;  //设置菜单为gprs模式
      led = Led_DevGsm;
      process_start(&DispLed_Process, (process_data_t)&led);
    } else if (pin0 == GPIO_PIN_SET && pin1 == GPIO_PIN_RESET) {  // wifi
	  printf("wifi version\r\n");
      netdevice_type = net_device_wifi;

      etimer_set(&et, 500);
      PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

      NetApi_Init(Net_Wifi);
      FIFO_Init(&mqtt_fifo, mqttdata_fifo, sizeof(sMqttData), 32);
      process_start(&WifiInit_Process, NULL);

      SetMenu.count = sizeof(menu_wifi) / sizeof(menu_wifi[0]);
      SetMenu.menu = (uint8_t *)menu_wifi;  //设置菜单为wifi模式

      led = Led_DevWifi;
      process_start(&DispLed_Process, (process_data_t)&led);
    } else {  //单机
	  printf("single version\r\n");
      netdevice_type = net_device_none;

      SetMenu.count = sizeof(menu_single) / sizeof(menu_single[0]);
      SetMenu.menu = (uint8_t *)menu_single;  //设置菜单为单机模式
      led = Led_DevSingle;
      process_start(&DispLed_Process, (process_data_t)&led);
    }
  }
  PROCESS_END();
}

